# -*- coding: utf-8 -*-
# 创建时间：2021/7/19 11:32
from bs4 import BeautifulSoup
import requests
import time, math, random, json
import pymysql
import re

__author__ = 'LuckyHhy'
# 获取商品列表
def search_keyword(keyword):
    uri = 'https://you.163.com/xhr/search/search.json'
    query = {
        "keyword": keyword,
        "page": 1
    }
    try:
        res = requests.get(uri, params=query).json()
        result = res['data']['directly']['searcherResult']['result']
        product_id = []
        for r in result:
            product_id.append(r['id'])
        return product_id
    except:
        raise



# 获取评论
def details(product_id):
    url = 'https://you.163.com/xhr/comment/listByItemByTag.json'
    try:
        C_list = []
        for i in range(1, 2):
            query = {
                "itemId": product_id,
                "page": i,
            }
            res = requests.get(url, params=query).json()
            if not res['data']['commentList']:
                break
            print("爬取第 %s 页评论" % i)
            commentList = res['data']['commentList']
            #print(commentList)
            C_list.extend(commentList)
            time.sleep(1)

        return C_list
    except:
        raise



def main():
    product_id = search_keyword('男士内裤')
    r_list = []
    for p in product_id:
        r_list.extend(details(p))

    #str={'skuInfo': ['尺码:L', '颜色:（深灰+藏青）2条'], 'frontUserName': '用****9', 'frontUserAvatar': None, 'content': '回购的产品。很好', 'createTime': 1605698806268, 'picList': ['https://yanxuan.nosdn.127.net/73fc9584d842b7db223897fe8fe683b0.jpg?watermark&type=2&gravity=southeast&dissolve=80&font=c2ltZmFuZw==&fontsize=400&fontcolor=d2hpdGU=&text=eXgxNDU1NTIwNDE='], 'commentReplyVO': None, 'memberLevel': 4, 'appendCommentVO': None, 'star': 5, 'itemId': 1021020}
    # print(r_list)
    # exit()
    with open('./briefs.txt', 'w') as f:
        for r in r_list:
            print(r)
            # try:
            #     f.write(json.dumps(r, ensure_ascii=False) + '\n')
            # except:
            #     print('出错啦')


if __name__ == '__main__':
    main()
